﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace DesktopClient {
	public partial class EasyChatClient : Form {

        private Form parent;
        private Form grandParent;
        private string userName;
        private string room;
        private bool guard;
        private DesktopClient.ServerProvider.ArrayOfAnyType[] messageMasc;
        private String[] userMasc;
        private DateTime loginTime;
        //private 
        private ServerProvider.ServiceSoapClient cl = new DesktopClient.ServerProvider.ServiceSoapClient();

		public EasyChatClient(Form parent, Form grandParent, string userName, string room) {
            
            this.parent    = parent;
            this.grandParent = grandParent;
            this.userName  = userName;
            this.room      = room;
            this.guard     = true;
            this.loginTime = cl.GetDateTimeNow();

			InitializeComponent();

            scaner.WorkerReportsProgress      = true;
            scaner.WorkerSupportsCancellation = true;
		}

        private void logout_Click(object sender, EventArgs e)
        {
            scaner.CancelAsync();
            cl.LogOut(this.userName);
            parent.Visible = true;
            grandParent.Visible = true;
            parent.Close();
            this.Close();
        }

        private void getUserList()
        {
            usersList.Text = "";
            foreach (string usr in cl.GetLoggedUsers(room))
            {
                usersList.AppendText(usr + Environment.NewLine);
            }
        }

        private void getMessage()
        {
            bool messageGuard = true;
            var mList = cl.GetNewMessages(this.loginTime, room);
            chat.Text = "";
            for (int i = 0; i < mList.Length; i++)
            {
                    chat.AppendText(mList[i][0].ToString() + " (" + mList[i][2].ToString() + "): " + mList[i][1].ToString() + Environment.NewLine);
                    messageGuard = false;
            }
        }

        private void setForm(object sender, EventArgs e)
        {
            this.Text = "EasyChat | User : " + userName;
            this.messageMasc = cl.GetNewMessages(this.loginTime, room);
            this.userMasc = null;
            scaner.RunWorkerAsync();
        }

        private void onExit(object sender, FormClosedEventArgs e)
        {
            this.logout_Click(sender, e);
        }

        private void send_Click(object sender, EventArgs e)
        {
            if (message.Text.Length > 0)
            {
                cl.PostMessage(message.Text, this.userName);
                message.Clear();
            }
        }

        private void Send(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.send_Click(sender, e);
            }
        }

        private void scaner_DoWork(object sender, DoWorkEventArgs e)
        {
            while (true)
            {
                if (scaner.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }
                Thread.Sleep(1000);
                scaner.ReportProgress(1);
            }
        }

        private void scaner_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.getUserList();
            this.getMessage();
        }

        private void ClearMessage(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                message.Clear();
            }
        }
	}
}
